(function (doc, win) {
    var docEl = doc.documentElement,
        resizeEvt = 'orientationchange' in window ? 'orientationchange' : 'resize',
        recalc = function () {
            var clientWidth = docEl.clientWidth;
            if (!clientWidth) return;
            docEl.style.fontSize = 20 * (clientWidth / 320) + 'px';
        };

    if (!doc.addEventListener) return;
    win.addEventListener(resizeEvt, recalc, false);
    doc.addEventListener('DOMContentLoaded', recalc, false);
})(document, window);


$(function(){
    setTimeout(function(){
        $('#m-index-1').animate({'scrollTop':$('#m-index-1')[0].scrollHeight},300);
    },3000);
    var my_mobiel =  $("#my_mobiel_old").val();
    var openid =  $("#openid").val();
    var nickname = $("#nickname").val();
    var headimgurl =  $("#headimgurl").val();
    var base_url = $("#base_url").val();
    var is_tuan = $("#is_tuan").val();
    var guoqi = $("#guoqi").val();
    var cg = $("#cg").val();
    if(guoqi == '1')
    {
        newFail();
    }

    if(cg == '1')
    {
        $('.btn-buy').removeClass('disabled');
        resSuc();
    }
    //关闭所有
    function closeAll(){
        $('.dialog').hide();
        $('#m-index-1 .bg').removeClass('blur');
    }
    //打开查看规则
    function showHelp(){
        $('#m-index-1 .bg').addClass('blur');
        $('.dg-help').show();
    }
    //非会员
    function noVip(){
        $('#m-index-1 .bg').addClass('blur');
        $('.dg-no-vip').show();
    }
    //打开参团填写本人号码
    function inStep1(){
        $('#m-index-1 .bg').addClass('blur');
        $('.dg-in1').show();
    }
    //打开参团填写团长号码
    function inStep2(){
        $('.dg-in1').hide();
        $('.dg-in2').show();
        event.stopPropagation();
    }
    //参团申请成功
    function inSuc(){
        $('.dg-in2').hide();
        $('.dg-in-suc').show();
    }
    //参团申请失败
    function inFail(){
        $('.dg-in2').hide();
        $('.dg-in-fail').show();
    }
    //参团成功
    function resSuc(){
        $('.dg-res-suc').show();
    }
    ////参团失败
    //function resFail(){
    //    $('.dg-res-fail').show();
    //}
    //创建团
    function newTeam(){
        $('#m-index-1 .bg').addClass('blur');
        $('.dg-new').show();
    }
    //创建成功
    function newSuc(){
        $('.dg-new').hide();
        $('.dg-new-suc').show();
    }
    //创建失败
    function newFail(){
        $('.dg-new').hide();
        $('.dg-in-fail').show();
    }

    //活动结束
    function timeOver(){
        $('#m-index-1 .bg').addClass('blur');
        $('.dg-over').show();
    }

    //满员
    function memberFull(){
        $('#m-index-1 .bg').addClass('blur');
        $('.dg-full').show();
    }


    //显示我的团
    function myTeam(){
        $('#m-index-1 .bg').addClass('blur');
        $('.dg-myTeam').show();
    }
    //禁用按钮
    function disable(){
        $('.tuan-but').addClass('disabled');
    }

    //启用按钮
    function enable(){
        $('.tuan-but').removeClass('disabled');
    }

    $('.dialog').on('click','.close',function(){
        closeAll();
    });
    $('.help').click(function(){
        showHelp();
    });
    //点击我要参团
    //var isVip = false;
    $('.btn-in').click(function(){
        if($(this).hasClass('disabled')){
            return false;
        }
        if(guoqi == '0' && is_tuan == '1')
        {
            inSuc();
            return false;
        }
        if(my_mobiel == '')
        {
            inStep1();
        }else{
            inStep2()
        }

    });

    //点击头像
    $('.member .txbox').each(function(){
        $(this).click(function(){
            myTeam();
        });
    })
    //填写本人号码点击确定
    $('.dg-in1 .getbtn').click(function(event){
        check_mobiel( $("#my_mobiel").val());
        inStep2();
    });
    //填写团长电话号码点击确定
    //inSuccess
    //var inSuccess = false;
    $('.dg-in2 .getbtn').click(function(event){
        check_mobiel($("#tuan_mobile").val());
        join_tuan();
        //if(inSuccess){
        //    inSuc();
        //}else{
        //    inFail();
        //}
        event.stopPropagation();
    });
    //用"我要购买"的按钮来触发两个页面，只是用来展示页面用的。
    //resSuccess
    var resSuccess = true;
    $('.btn-buy').click(function(event){
        $('#m-index-1 .bg').addClass('blur');
        if(cg == '1')
        {
            location.href = "http://movie.js118114.com:8088/weixin/view/preSellTicket/preSellTicket_uri.jsp";
        }
        event.stopPropagation();
    });
    //点击我要建团
    $('.btn-new').click(function(){
        if($(this).hasClass('disabled')){
            return false;
        }

        if(guoqi == '0' && is_tuan == '1')
        {
            newSuc();
            return false;
        }
        newTeam();
    });
    //我要建团 确定
    $('.dg-new .getbtn').click(function(event){
        build_tuan()
        event.stopPropagation();
    });
    $('#m-index-1').on('click','.dialog',function(event){
        closeAll();
        return false;
    });
    $('#m-index-1 .dialog img,#m-index-1 .dialog input').click(function(event) {
        event.stopPropagation();
    });

    //建团
    function build_tuan()
    {
        disable();
        if( $("#my_tuan_mobiel").val() != '')
        {
            check_mobiel($("#my_tuan_mobiel").val());
            my_mobiel = $("#my_tuan_mobiel").val();
        }

        $.ajax({
            url: base_url+"/index.php/m/index/build_tuan",
            data: 'open_id='+openid+'&mobile='+my_mobiel+'&nickname='+nickname+'&headimgurl='+headimgurl,
            type: 'get',
            success: function (msg) {
                var obj = JSON.parse(msg);
                if(obj.code == 1){
                    $(".nums").html(obj.data.count);
                    $(".tz_phone").html(obj.data.mobile);
                    $("#headimgurl_ul").append(obj.data.headimgurl);
                    is_tuan = 1;
                    closeAll()
                    newSuc();
                }else if(obj.code == 2)
                {
                    closeAll()
                    newSuc();
                }else if(obj.code == 3)
                {
                    closeAll()
                    timeOver();
                }else{
                    alert(obj.data);
                }
                enable();
            }
        });

    }
    //加团
    function join_tuan()
    {
        disable();
        if( $("#my_mobiel").val() != '')
        {
            check_mobiel($("#my_mobiel").val());
            my_mobiel = $("#my_mobiel").val();
        }
        var  tz_mobile = $("#tuan_mobile").val();
        check_mobiel(tz_mobile);
        $.ajax({
            url: base_url+"/index.php/m/index/join_tuan",
            data: 'open_id='+openid+'&mobile='+my_mobiel+'&nickname='+nickname+'&headimgurl='+headimgurl+'&tz_mobile='+tz_mobile,
            type: 'get',
            success: function (msg) {
                var obj = JSON.parse(msg);
                if(obj.code == 1){
                    $(".nums").html(obj.data.count);
                    $(".tz_phone").html(obj.data.mobile);
                    $("#headimgurl_ul").append(obj.data.headimgurl);
                    is_tuan = 1;
                    closeAll()
                    if(obj.data.count == 8)
                    {
                        cg =1;
                        $('.btn-buy').removeClass('disabled');
                        resSuc();
                    }else{
                        inSuc();
                    }
                }else if(obj.code == 2)
                {
                    closeAll()
                    inSuc();
                }else if(obj.code == 3)
                {
                    closeAll()
                    timeOver();
                }else if(obj.code == 6)
                {
                    closeAll()
                    memberFull();
                }else{
                    alert(obj.data);
                }
                enable();
            }
        });

    }


    function check_mobiel(my_mobiel)
    {
        if(my_mobiel == '')
        {
            alert("手机号不能为空");
            return false;
        }
        if (!my_mobiel.match(/^(((13[0-9]{1})|(15[0-9]{1})|(18[0-9]{1}))+\d{8})$/)) {
            alert("手机号码格式不正确！");
            return false;
        }

    }

});